<!--
 * @Description: 
 * @Autor: luojie
 * @Date: 2021-07-02 20:18:49
 * @LastEditors: luojie
 * @LastEditTime: 2021-08-08 19:13:25
-->
<template>
  <view>
    <HasAuth v-if="isAuth"/>
    <Login :isSIN="isSIN" :phone="phone" :isPad="isPad" @showPadLogin="showPadLoginHandle" :wxCode="wxCode" @updateWxCode="updateWxCode" v-if="!showPadLogin && !isAuth"/>
    <PadLogin v-if="showPadLogin && !isAuth" @hidePadLogin='hidePadLoginHandle'/>
  </view>
</template>

<script>
import HasAuth from './hasAuth'  // 已登陆并经认证拥有权限显示。
import Login from './login'  // 未登陆 非 pad端显示
import PadLogin from './padLogin' // 未登录 pad端显示
import { mapState } from 'vuex';
import {getPhoneBySIN, login, getWxBindPhone,wxLogin} from '@/api/login'
export default {
  onLoad(option) {
    console.log('=====调用onLoad方法了========')
    // uni.login({
    //   provider: "weixin",
    //   success: (res) => {
    //     this.$store.commit('SET_WXCODE',res.code)
    //     console.log('====生成新的wxCode======', this.wxCode)
    //   }
    // });
    // this.$store.commit('SET_AUTH_STATUS', this.isSIN)
    // let code = 'cA3uiYwCvl9qWd3hBwJ5Fdvl9aiNMLAK'
    let shenAiNin = uni.getStorageSync('shenAiNin') || {}
    if (option.code) {
      // 深i您跳转进来的
      getPhoneBySIN(option.code).then(rs=>{
        this.phone = rs.mobileNum
        uni.setStorage({key:'telPhone',data:rs.mobileNum});
        uni.setStorage({key:'shenAiNin',data:rs})
        this.isSIN = true
      })
    }else if(shenAiNin.mobileNum){
      this.phone = shenAiNin.mobileNum
      uni.setStorage({key:'telPhone',data:shenAiNin.mobileNum});
      this.isSIN = true
    }else{
      uni.removeStorageSync('shenAiNin');
    }
    this.getUserAgent()
  },
  watch:{
    isAuth(value){
      this.$store.commit('SET_AUTH_STATUS', value)
    }
  },
  onShow(){
    // 清除选房筛选数据
    uni.removeStorageSync('curSelectedFloor');
    uni.removeStorageSync('curSearchValue');
    uni.removeStorageSync('scrollViewTop');
    uni.removeStorageSync('checkedList');
  },
  methods: {
    updateWxCode(code){
      this.$store.commit('SET_WXCODE',code)
      // this.wxCode=code;
    },
    getUserAgent(){
      //判断用户是使用手机还是ipad进入页面
      return new Promise((resolve) =>{
        uni.getSystemInfo({
          success: (res) => {
            // if(res.windowWidth >750 ){
              console.log('设备信息===', res)
            // if(this.judgetIpad(res.screenWidth, res.screenHeight)){
            //   this.isPad = true;
            //   resolve(true)
            // } else {
            //   this.isPad = false
            //   resolve(false)
            // }


              this.isPad = this.judgetIpad2(res);
              resolve(this.isPad)

          }
        });
      })
    },
    showPadLoginHandle() {
      this.showPadLogin = true
    },
    hidePadLoginHandle() {
      this.showPadLogin = false
    },

    judgetAndroidpad(screenWidth, screenHeight){
      const screenRate = screenWidth / screenHeight
      return screenRate >= 0.625;  // Android宽高比大于0.625判定为pad
    },

    judgetIpad2(res){
      if(res.platform=='android' && this.judgetAndroidpad(res.screenWidth, res.screenHeight) ) {
        return true;
      }else if(res.platform=='ios' && res.screenWidth>750 ){
        return true;
      }
      return false;
    },

  },
  computed: {
    ...mapState(['isAuth','wxCode'])
  },
  data() {
    return {
      isPad: true, // 判断是否是平板端。 
      isSIN: false, // 判断是否是深i您登陆
      phone: '', // 登录手机号
      showPadLogin: false,
      // wxCode: ''
    }
  },
  components: {
    HasAuth,
    Login,
    PadLogin
  },
}
</script>

<style>

</style>